package com.phonegap.gwt.geolocation.client.impl;



//http://docs.phonegap.com/docs/geolocation

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.phonegap.gwt.geolocation.client.Geolocation;
import com.phonegap.gwt.geolocation.client.Position;
import com.phonegap.gwt.geolocation.client.PositionCallback;
import com.phonegap.gwt.geolocation.client.PositionOptions;



public class GeolocationImpl  {



	  public native boolean isSupported() /*-{
	    return !!$wnd.navigator.geolocation;
	  }-*/;
	  //    return navigator.geolocation;
	  
	//navigator.geolocation.getCurrentPosition(win, fail);
	  public native Geolocation getGeolocation() /*-{

	    return $wnd.navigator.geolocation;
	  }-*/;
	  
	  protected native void _clearWatch(Geolocation geo,int watchId) /*-{
	    geo.clearWatch(watchId);
  }-*/;
	  
	  
	  public void clearWatch(Geolocation geo,
			  int watchId) {
		  _clearWatch(geo,watchId);
		  }
	  
	  
	  public void getCurrentPosition(Geolocation geo,
		      PositionCallback callback) {
		  _getCurrentPosition(geo, callback, null);
		  }
	  
	  public void getCurrentPosition(Geolocation geo,
		      PositionCallback callback, PositionOptions options) {
		    _getCurrentPosition(geo, callback, options);
		  }
	  public int  watchPosition(Geolocation geo,
		      PositionCallback callback) {
		 return _watchPosition(geo, callback, null);
		  }
	  
	  public int watchPosition(Geolocation geo,
		      PositionCallback callback, PositionOptions options) {
		  return  _watchPosition(geo, callback, options);
		  }
	  
	  
	  protected native int _watchPosition(Geolocation geo,
		      PositionCallback callback, PositionOptions options) /*-{
		   var watchId = geo.watchPosition(
		      function(position) {
		        @com.phonegap.gwt.geolocation.client.impl.GeolocationImpl::handleSuccess(Lcom/phonegap/gwt/geolocation/client/PositionCallback;Lcom/phonegap/gwt/geolocation/client/Position;) (callback, position);
		      },
		      function(error) {
		        @com.phonegap.gwt.geolocation.client.impl.GeolocationImpl::handleError(Lcom/phonegap/gwt/geolocation/client/PositionCallback;Ljava/lang/String;) (callback, error);
		      },
		      options
		    );
		  
		  	return watchId;
		  }-*/;
	  
	  
	  protected native void _getCurrentPosition(Geolocation geo,
		      PositionCallback callback, PositionOptions options) /*-{
		    geo.getCurrentPosition(
		      function(position) {
    var accuracy = position.coords.accuracy;
		        @com.phonegap.gwt.geolocation.client.impl.GeolocationImpl::handleSuccess(Lcom/phonegap/gwt/geolocation/client/PositionCallback;Lcom/phonegap/gwt/geolocation/client/Position;) (callback, position);
		      },
		      function(error) {
		        @com.phonegap.gwt.geolocation.client.impl.GeolocationImpl::handleError(Lcom/phonegap/gwt/geolocation/client/PositionCallback;Ljava/lang/String;) (callback, error);
		      },
		      options
		    );
		  }-*/;

	  

	  protected static final void handleSuccess(PositionCallback callback,
			  Position position) {
		    UncaughtExceptionHandler ueh = GWT.getUncaughtExceptionHandler();
		    if (ueh != null) {
		      try {
		        callback.onSuccess(position);
		      } catch (Throwable t) {
		        ueh.onUncaughtException(t);
		      }
		    } else {
		      callback.onSuccess(position);
		    }
		  }

		  /*
		   * Helper method for the getCurrentPosition() / watchPosition() methods
		   */
		  protected static final void handleError(PositionCallback callback,
		      String error) {
		    UncaughtExceptionHandler ueh = GWT.getUncaughtExceptionHandler();
		    if (ueh != null) {
		      try {
		        callback.onFailure(error);
		      } catch (Throwable t) {
		        ueh.onUncaughtException(t);
		      }
		    } else {
		      callback.onFailure(error);
		    }
		  }
		  
		  
}
